package main

import (
	"fmt"
	"github.com/the404/letcode/common"
)

// 这个问题很有趣,编译器中判断是否有完整的输入，是使用栈来解决的
func main() {
	fmt.Println(isValid("()"))
}

func isValid(s string) bool {
	if len(s)%2 != 0 {
		return false
	}
	arr := []rune(s)
	stack := &common.Stack{}

	for i := 0; i < len(arr); i++ {
		if isLeft(arr[i]) {
			stack.Push(arr[i])
		} else {
			c1 := stack.Pop()
			c2 := arr[i]
			if !isEquals(c1, c2) {
				return false
			}
		}
	}
	return stack.Empty()
}

func isLeft(v rune) bool {
	return v == '(' || v == '[' || v == '{'
}

func isEquals(c1, c2 rune) bool {
	if c1 == '(' {
		return c2 == ')'
	}
	if c1 == '[' {
		return c2 == ']'
	}
	if c1 == '{' {
		return c2 == '}'
	}
	return false
}


